﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.Mail;
using CE.User;
using CE.Global;
using CE.Security;
using CE.InputOut;
using CE.Database;
using CE.CarInsurance;
using CXDecryption;

public partial class carinsurance_merchant_MerchantCustomerList : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            btnSearch.Focus();
            txtCEId.Text = "0";
            if (Request.QueryString["cid"] != null)
            {
                txtCEId.Text = Request.QueryString["cid"].ToString();
                LoadGrid(1);
            }
            else
            {
                LoadGrid(2);
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            grvCustomer.SelectedIndex = 0;

            divCar.Visible = false;
            grvMerchantCar.DataSource = null;
            grvMerchantCar.DataBind();

            if (txtSearch.Text.Trim().Length > 5 || txtSearchRefNo.Text.Trim().Length > 5)
            {
                LoadGrid(2);
            }
            else if (txtCEId.Text != "0")
            {
                LoadGrid(1);
            }
            else
            {
                grvCustomer.DataSource = null;
                grvCustomer.DataBind();
            }
        }
        catch (Exception ex)
        {
        }
    }

    //OnSelectedIndexChanged="grvCustomer_SelectedIndexChanged"
    //protected void grvCustomer_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (grvCustomer.Rows.Count > 0)
    //    {
    //        Response.Redirect("MerchantUser.aspx?CEId=" + grvCustomer.SelectedDataKey.Value);
    //    }
    //}

    protected void grvCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName.Equals("MyDelete"))
        //{
        //    if (e.CommandArgument != null)
        //    {
        //        Int64 CEId = 0;
        //        Int64 RefCEUserId = 0;
        //        Int64 _result = -1;
        //        CEId = Convert.ToInt64(e.CommandArgument);
        //        RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
        //        MerchantUserDB _merchantUserDB = new MerchantUserDB();
        //        _result = _merchantUserDB.Delete(CEId, RefCEUserId);
        //        if (_result > 0)
        //        {
        //            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Delete was successful.');", true);
        //        }
        //        else
        //        {
        //            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Delete failed.');", true);
        //        }
        //        btnSearch_Click(null, null);
        //    }
        //}
        //if (e.CommandName.Equals("MyCar"))
        //{
        //    if (e.CommandArgument != null)
        //    {
        //        Response.Redirect("NewMerchantCar.aspx?CEId=" + e.CommandArgument.ToString());
        //    }
        //}
        //if (e.CommandName.Equals("MyFile"))
        //{
        //    if (e.CommandArgument != null)
        //    {
        //        Response.Redirect("MerchantCarFiles.aspx?CEId=" + e.CommandArgument.ToString());
        //    }
        //}
        if (e.CommandName.Equals("MyCar"))
        {
            if (e.CommandArgument != null)
            {
                LoadCarGrid(Convert.ToInt64(e.CommandArgument.ToString()));
                foreach (GridViewRow myRow in grvCustomer.Rows)
                {
                    Int64 _CarId = Convert.ToInt64(grvCustomer.DataKeys[myRow.RowIndex][0]);
                    if (_CarId == Convert.ToInt64(e.CommandArgument.ToString()))
                    {
                        grvCustomer.SelectedIndex = myRow.RowIndex;
                        break;
                    }
                }
            }
        }
        //if (e.CommandName.Equals("MyQuote"))
        //{
        //    if (e.CommandArgument != null)
        //    {
        //        Response.Redirect("MerchantManualQuote.aspx?CEId=" + e.CommandArgument.ToString());
        //    }
        //}
    }

    private void LoadCarGrid(Int64 CEId)
    {
        divCar.Visible = true;
        DataSet _dsFleetCar = new DataSet();
        MyDatabase _db = new MyDatabase("GG_MCDBConnectionString");
        string _spName = "sprocMerchantCarAndPolicyDetailSelectItemList";
        SortedList _spPara = new SortedList();
        _spPara.Add("@RefCEUserId", CEId);
        _dsFleetCar = _db.GetDataSet(_spName, _spPara);
        if (_dsFleetCar != null && _dsFleetCar.Tables.Count > 0)
        {
            grvMerchantCar.DataSource = _dsFleetCar.Tables[0];
            grvMerchantCar.DataBind();
        }
        else
        {
            grvMerchantCar.DataSource = null;
            grvMerchantCar.DataBind();
        }
    }

    private void LoadGrid(Int16 LaodId)
    {
        DataSet _dsMerchantUser = new DataSet();
        MerchantUserDB _merchantUserDB = new MerchantUserDB();
        if (LaodId == 1)
        {
            _dsMerchantUser = _merchantUserDB.GetUserSearchById(Convert.ToInt64(txtCEId.Text.Trim()));
        }
        else if (LaodId == 2)
        {
            _dsMerchantUser = _merchantUserDB.GetUserSearch(Server.HtmlEncode(txtSearch.Text.Trim()), Server.HtmlEncode(txtSearchRefNo.Text.Trim()));
        }
        if (_dsMerchantUser != null && _dsMerchantUser.Tables.Count > 0)
        {
            grvCustomer.DataSource = _dsMerchantUser.Tables[0];
            grvCustomer.DataBind();
        }
        else
        {
            grvCustomer.DataSource = null;
            grvCustomer.DataBind();
        }
    }

    protected void grvMerchantCar_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("MyQuote"))
        {
            if (e.CommandArgument != null)
            {
                string CEId = "";
                foreach (GridViewRow myRow in grvMerchantCar.Rows)
                {
                    Int64 _CarId = Convert.ToInt64(grvMerchantCar.DataKeys[myRow.RowIndex][0]);
                    if (_CarId == Convert.ToInt64(e.CommandArgument.ToString()))
                    {
                        CEId = grvMerchantCar.DataKeys[myRow.RowIndex][2].ToString();
                        break;
                    }
                }
                if (CEId != "")
                    Response.Redirect("MerchantManualQuote.aspx?CEId=" + CEId + "&CarId=" + e.CommandArgument.ToString());
            }
        }

    }

    protected void btnAppDtl_Click(object sender, EventArgs e)
    {
        Response.Redirect("MerchantApplication.aspx");
    }
    protected void btnAppSum_Click(object sender, EventArgs e)
    {
        Response.Redirect("MerchantApplicationStatus.aspx");
    }
}
